package cn.b.training.baby_education_training.api;

import cn.b.training.baby_education_training.entity.BabyInfo;
import cn.b.training.baby_education_training.entity.BabyWeight;
import cn.b.training.baby_education_training.entity.UserInfo;
import cn.b.training.baby_education_training.repository.BabyWeightRepository;
import cn.b.training.baby_education_training.service.BabyInfoService;
import cn.b.training.baby_education_training.service.BabyWeightService;
import cn.b.training.baby_education_training.service.UserInfoService;
import cn.b.training.baby_education_training.service.dto.BabyWeightDto;
import com.alibaba.fastjson.JSONObject;
import org.springframework.data.domain.Page;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.User;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * @program: baby_education_training
 * @description
 * @author: wuying
 * @create: 2022-12-01 09:57
 **/
@RestController
@RequestMapping("/weight")
public class BabyWeightController {
    private final UserInfoService userInfoService;
    private final BabyWeightService babyWeightService;
    private final BabyWeightRepository babyWeightRepository;
    private final BabyInfoService babyInfoService;

    public BabyWeightController(UserInfoService userInfoService, BabyWeightService babyWeightService, BabyWeightRepository babyWeightRepository, BabyInfoService babyInfoService) {
        this.userInfoService = userInfoService;
        this.babyWeightService = babyWeightService;
        this.babyWeightRepository = babyWeightRepository;
        this.babyInfoService = babyInfoService;
    }

    @PostMapping("/insert/weight")
    public ResponseEntity insertBaby(@RequestParam Long babyId,
                                     @RequestParam String recordTime,
                                     @RequestParam float weight){
        try{
            Authentication user= SecurityContextHolder.getContext().getAuthentication();
            User currentUser=(User)user.getPrincipal();
            UserInfo userInfo=this.userInfoService.getUser(currentUser.getUsername());
            if(userInfo!=null){
                BabyWeight babyWeight = this.babyWeightService.insertWeight(recordTime,weight);
                BabyInfo babyInfo = babyInfoService.getBabyById(babyId);
                if (babyInfo!=null){
                    babyWeight.setBabyId(babyId);
                    babyWeightRepository.save(babyWeight);
                }
                //babyInfo = babyInfoRepository.save(babyInfo);
                return ResponseEntity.ok(babyWeight);
            }else{
                JSONObject error = new JSONObject();
                error.put("error","登录身份不合法");
                return ResponseEntity.status(400).body(error);
            }
        }catch(Exception e){
            JSONObject error = new JSONObject();
            error.put("error",e.getMessage());
            return ResponseEntity.status(400).body(error);
        }
    }

    @PutMapping("/update/weight")
    public ResponseEntity updateWeight(@Valid BabyWeightDto weightDto){
        try {
            boolean b = babyWeightService.updateWeightInfo(weightDto);
            if (b){
                JSONObject result = new JSONObject();
                result.put("success","修改成功");
                return ResponseEntity.ok(result);
            }else {
                JSONObject error = new JSONObject();
                error.put("failed","修改失败");
                return ResponseEntity.status(400).body(error);
            }
        } catch (Exception e) {
            e.printStackTrace();
            JSONObject error = new JSONObject();
            error.put("error",e.getMessage());
            return ResponseEntity.status(400).body(error);
        }
    }

    @DeleteMapping("/delete/weight")
    public ResponseEntity deleteWeight(@RequestParam Long id){
        try {
            boolean b = babyWeightService.deleteWeight(id);
            if (b){
                JSONObject result = new JSONObject();
                result.put("success","删除成功");
                return ResponseEntity.ok(result);
            }else {
                JSONObject error = new JSONObject();
                error.put("failed","删除失败");
                return ResponseEntity.status(400).body(error);
            }
        } catch (Exception e) {
            e.printStackTrace();
            JSONObject error = new JSONObject();
            error.put("error",e.getMessage());
            return ResponseEntity.status(400).body(error);
        }
    }

    @GetMapping("/get/id")
    public ResponseEntity getWeight(@RequestParam Long id){
        try {
            return ResponseEntity.ok(babyWeightService.getWeightById(id));
        } catch (Exception e) {
            e.printStackTrace();
            JSONObject error = new JSONObject();
            error.put("error",e.getMessage());
            return ResponseEntity.status(400).body(error);
        }
    }

    @GetMapping("/get/babyId")
    public ResponseEntity getBabyHeight(@RequestParam Long babyId){
        try {
            return ResponseEntity.ok(babyWeightService.getWeightByBabyId(babyId));
        } catch (Exception e) {
            e.printStackTrace();
            JSONObject error = new JSONObject();
            error.put("error",e.getMessage());
            return ResponseEntity.status(400).body(error);
        }
    }

    @GetMapping("/all")
    public ResponseEntity getWeights(@RequestParam(required = false, defaultValue = "0") Integer page,
                                     @RequestParam(required = false, defaultValue = "10") Integer size) {
        Page<BabyWeight> result = this.babyWeightService.getAllWeights(page, size);
        return ResponseEntity.ok(result);
    }
}
